package com.job.admin.audit.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.job.admin.audit.mapper.AuditAvatarMapper;
import com.job.admin.audit.service.AuditAvatarService;
import com.job.admin.model.audit.constant.StatusConstant;
import com.job.admin.model.audit.dto.AuditAvatarPageDto;
import com.job.admin.model.audit.pojo.AuditAvatar;
import com.job.client.common.result.AppHttpCode;
import com.job.client.common.result.ResponseResult;
import com.job.client.common.utils.DateTimeUtils;
import com.job.client.model.rabbitmq.AuditResultConstant;
import com.job.feign.client.auth.AuthFeign;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AuditAvatarServiceImpl extends ServiceImpl<AuditAvatarMapper, AuditAvatar> implements AuditAvatarService {
    @Autowired
    private AuditAvatarMapper auditAvatarMapper;
    @Autowired
    private AuthFeign authFeign;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public ResponseResult getList(Integer page, Integer size, AuditAvatarPageDto dto) {
        if(page==null){
            page=1;
        }
        if(size==null){
            size=5;
        }
        QueryWrapper<AuditAvatar> queryWrapper=new QueryWrapper<>();
        if(dto==null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        if(!("".equals(dto.getAuditUser()))){
            queryWrapper.eq("audit_user",dto.getAuditUser());
        }
        if(!("".equals(dto.getAvatarUser()))){
            queryWrapper.eq("avatar_user",dto.getAvatarUser());
        }
        if(!("".equals(dto.getAvatarStatus()) || dto.getAvatarStatus()==null)){
            queryWrapper.eq("status",dto.getAvatarStatus());
        }else{
            queryWrapper.eq("status",StatusConstant.STATUS_TO_REVIEWED);
        }
        Page<AuditAvatar> data=new Page<>(page,size);
        auditAvatarMapper.selectPage(data,queryWrapper);
        return ResponseResult.OkResult(data);
    }

    @Override
    public ResponseResult getUserInfo(String userId) {
        ResponseResult result = authFeign.getUserInfoById(userId);
        if(result.getCode()!=200){
            return result;
        }
        return ResponseResult.OkResult(result.getData());
    }

    @Override
    public ResponseResult auditSuccess(String avatarId,String userNumber) {
        if(avatarId==null || userNumber==null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        UpdateWrapper<AuditAvatar> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("status", StatusConstant.STATUS_AUDIT_SUCCESS)
                .set("audit_user",userNumber)
                .set("audit_time", DateTimeUtils.createNowTime())
                .eq("avatar_id",avatarId);
        int i = auditAvatarMapper.update(updateWrapper);
        if(i!=1){
            return ResponseResult.ErrorResult(AppHttpCode.SERVICE_ERROR);
        }
        // 使用rabbitmq修改client中的头像信息
        Map<String,String> data = new HashMap<>();
        data.put("avatarId",avatarId);
        data.put("result",String.valueOf(StatusConstant.STATUS_AUDIT_SUCCESS)); //审核通过
        rabbitTemplate.convertAndSend(
                AuditResultConstant.EXCHANGE_AUDIT_RESULT,
                AuditResultConstant.ROUTINGKEY_AVATAR_RESUT,
                JSON.toJSONString(data));
        return ResponseResult.OkResult();
    }
    @Override
    public ResponseResult auditError(String avatarId, String userNumber,String errorMessage) {
        if(avatarId==null || userNumber==null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        UpdateWrapper<AuditAvatar> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("status", StatusConstant.STATUS_AUDIT_ERROR)
                .set("audit_user",userNumber)
                .set("reason",errorMessage)
                .set("audit_time", DateTimeUtils.createNowTime())
                .eq("avatar_id",avatarId);
        int i = auditAvatarMapper.update(updateWrapper);
        if(i!=1){
            return ResponseResult.ErrorResult(AppHttpCode.SERVICE_ERROR);
        }
        // 使用rabbitmq修改client中的头像信息
        Map<String,String> data = new HashMap<>();
        data.put("avatarId",avatarId);
        data.put("result", String.valueOf(StatusConstant.STATUS_AUDIT_ERROR)); //审核未通过
        rabbitTemplate.convertAndSend(
                AuditResultConstant.EXCHANGE_AUDIT_RESULT,
                AuditResultConstant.ROUTINGKEY_AVATAR_RESUT,
                JSON.toJSONString(data));
        return ResponseResult.OkResult();
    }
    @Override
    public ResponseResult getAvatarResultById(String avatarId) {
        if(avatarId==null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        AuditAvatar auditAvatar = auditAvatarMapper.selectById(avatarId);
        if(auditAvatar == null){
            return ResponseResult.ErrorResult(AppHttpCode.SERVICE_ERROR);
        }
        return ResponseResult.OkResult(auditAvatar);
    }

    /*
    * 获得该用户未审核的数据
    * */
    public ResponseResult getAuditInfo(String userId){
        if(userId == null){
            return ResponseResult.ErrorResult(AppHttpCode.DATA_NULL);
        }
        QueryWrapper<AuditAvatar> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("avatar_user",userId).eq("status",StatusConstant.STATUS_TO_REVIEWED);
        List<AuditAvatar> auditAvatarList = auditAvatarMapper.selectList(queryWrapper);
        return ResponseResult.OkResult(auditAvatarList);
    }
}
